package com.qyhl.webtv.module_news.news.comment;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.entity.news.NormalNewsBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/11/30.
 */

public class CommentModel implements CommentContract.CommentModel {
    private CommentPresenter mPresenter;


    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;

    public CommentModel(CommentPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }

    @Override
    public void getComment(String newsId, final String commentId) {


        EasyHttp.get(NewsUrl.GET_ALL_COMMENTS)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("newsId", newsId)
                .params("commentID", commentId)
                .execute(new SimpleCallBack<List<NormalNewsBean.ArticleComment>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if ("0".equals(commentId)) {
                                mPresenter.setError(2, "暂无任何评论！");
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        } else {
                            if ("0".equals(commentId)) {
                                mPresenter.setError(4, "解析出错，加载失败！");
                            } else {
                                mPresenter.setError(5, "解析出错，加载失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<NormalNewsBean.ArticleComment> articleComments) {
                        if (articleComments != null && articleComments.size() > 0) {
                            if ("0".equals(commentId)) {
                                mPresenter.setComment(articleComments, false);
                            } else {
                                mPresenter.setComment(articleComments, true);
                            }
                        } else {
                            if ("0".equals(commentId)) {
                                mPresenter.setError(2, "暂无任何评论！");
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        }
                    }
                });

    }

    @Override
    public void sendComment(String title, String catalogId, String type, String id, String commentUser, String content) {

        String url =  CommonUtils.getInstance().getCmsUrl();
        String headicon = CommonUtils.getInstance().getUserLogo();

        EasyHttp.post(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "addComment")
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("Title", content)
                .params("catalogId", catalogId)
                .params("catalogType", type)
                .params("articleId", id)
                .params("commentUser", commentUser)
                .params("content", content)
                .params("ip", "127.0.0.1")
                .params("logo", headicon)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.sendError("网络异常，跟帖失败，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.sendSuccess("跟帖成功，等待审核!");
                        } else {
                            mPresenter.sendError("跟帖失败，请稍后重试!");
                        }
                    }
                });

    }

    @Override
    public void getCoin() {
        intergralService.gainCoin("4","gainCoin", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCoinError(msg);
            }
        });
    }
}
